﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022   保留所有权利。
 * CLR版本：4.0.30319.42000
 * 机器名称：DESKTOP-UL84BU5
 * 公司名称：
 * 命名空间：KingerRobot.WanliMDC.SpiService.Services.BaseBussiness
 * 唯一标识：e699e016-7633-420b-9db7-a7508aeb14f4
 * 文件名：ProductImpl
 * 当前用户域：DESKTOP-UL84BU5
 * 
 * 创建者：zzl
 * 电子邮箱：1003590782@qq.com
 * 创建时间：2022/8/16 8:20:35
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
using KingerRobot.MexicoSailunMDC.Dao;
using KingerRobot.MexicoSailunMES.Spi.DTO;
using System.Drawing;
using System.Text.RegularExpressions;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace KingerRobot.MexicoSailunMDC.SpiService;

public class ProductImpl : IProductService
{
    private readonly static IMesProduct _MesProduct = new BMesProduct();
    private readonly static IProdProduct _ProdProduct = new BProdProduct();
    private readonly static IWmsSetting _WmsSetting = new BWmsSetting();
    private readonly static IProdMaterial _ProdMaterial = new BProdMaterial();






    public int GetMesNoSyncProduct(BcdInfoBean info)
    {
        var db = Db.Default;

        TireInfo tireInfo = new()
        {
            TireCode = info.BcdNo,
            MaterialCode = info.TbmSpec,
            MaterialDesc = info.TbmSpecDesc,
            Height = Convert.ToInt32(info.HEIGHT ?? 0),
            Size = GetSize(info.TbmSpecDesc),
            ProdDt = info.ProdDt.Value,
            TbmNo = info.TbmNo,
            TbmPs = info.TbmPs,
            InPermit = info.InPermit,
            TbmExpirationTime = info.TbmExpirationTime
        };
        var materialInfo = _ProdMaterial.AddProdMaterial(tireInfo);


        var mesProduct = db.MesProducts.Where(t => t.BcdNo == info.BcdNo).FirstOrDefault();

        if (mesProduct is not null)
        {
            WriteLog("开始修改mes胎胚数据", true);
            try
            {
                mesProduct.TbmSpec = info.TbmSpec;
                mesProduct.TbmSpecDesc = info.TbmSpecDesc;
                mesProduct.TbmSpecVer = info.TbmSpecVer;
                mesProduct.TbmExpirationTime = info.TbmExpirationTime;
                mesProduct.TbmPs = info.TbmPs;
                mesProduct.TbmNo = info.TbmNo;
                mesProduct.ProdDt = info.ProdDt;
                mesProduct.StdWt = info.StdWt;
                mesProduct.MinWt = info.MinWt;
                mesProduct.MaxWt = info.MaxWt;
                mesProduct.InPermit = info.InPermit;
                mesProduct.RcvFlag = info.RcvFlag;
                mesProduct.RegDt = info.RegDt;
                mesProduct.Rfid = info.Rfid;
                mesProduct.Relateequipmen = info.Relateequipmen;
                mesProduct.Remarks = info.Remarks;
                mesProduct.MaterialId = materialInfo.Id;
                mesProduct.MaterialCode = materialInfo.MaterialCode;
                mesProduct.Height = info.HEIGHT ?? 0;
                //mesProduct.BcdNo = info.BcdNo;

                //db.MesProducts.Update(mesProduct);
                db.SaveChanges();
                //return 1;
            }
            catch (Exception ex)
            {
                WriteLog($"开修改mes胎胚数据异常{ex.ToString()}", true);
                return 0;
            }
        }
        else
        {
            db.MesProducts.Insert(new POMesProduct
            {
                Id = XID.Nextval,
                BcdNo = info.BcdNo,
                TbmSpec = info.TbmSpec,
                TbmSpecDesc = info.TbmSpecDesc,
                TbmSpecVer = info.TbmSpecVer,
                TbmExpirationTime = info.TbmExpirationTime,
                TbmPs = info.TbmPs==null?"0": info.TbmPs,
                TbmNo = info.TbmNo,
                ProdDt = info.ProdDt,
                StdWt = info.StdWt,
                MinWt = info.MinWt,
                MaxWt = info.MaxWt,
                InPermit = info.InPermit,
                RcvFlag = info.RcvFlag,
                RegDt = info.RegDt,
                Rfid = info.Rfid,
                Relateequipmen = info.Relateequipmen,
                Remarks = info.Remarks,
                MaterialId = materialInfo.Id,
                MaterialCode = materialInfo.MaterialCode,
                Height = info.HEIGHT,
            });
            //return 1;
        }

        var prodProducts = db.ProdProducts.Where(t => t.ProductCode == info.BcdNo).ToList();
        if (prodProducts is not null)
        {
            WriteLog("开始修改prod胎胚数据", true);
            try
            {
                foreach (var prodProduct in prodProducts)
                {
                    prodProduct.TbmExpirationTime = info.TbmExpirationTime;
                    db.SaveChanges();
                }
                //return 1;
                //prodProduct.TbmSpec = info.TbmSpec;
                //prodProduct.TbmSpecDesc = info.TbmSpecDesc;
                //prodProduct.TbmSpecVer = info.TbmSpecVer;
              
                //prodProduct.TbmPs = info.TbmPs;
                //prodProduct.TbmNo = info.TbmNo;
                //prodProduct.ProdDt = info.ProdDt;
                //prodProduct.StdWt = info.StdWt;
                //prodProduct.MinWt = info.MinWt;
                //prodProduct.MaxWt = info.MaxWt;
                //prodProduct.InPermit = info.InPermit;
                //prodProduct.RcvFlag = info.RcvFlag;
                //prodProduct.RegDt = info.RegDt;
                //prodProduct.Rfid = info.Rfid;
                //prodProduct.Relateequipmen = info.Relateequipmen;
                //prodProduct.Remarks = info.Remarks;
                //prodProduct.MaterialId = materialInfo.Id;
                //prodProduct.MaterialCode = materialInfo.MaterialCode;
                //prodProduct.Height = info.HEIGHT ?? 0;
                //mesProduct.BcdNo = info.BcdNo;

                //db.MesProducts.Update(mesProduct);
                //db.SaveChanges();
                //return 1;
            }
            catch (Exception ex)
            {
                WriteLog($"开修改mes胎胚数据异常{ex.ToString()}", true);
                return 0;
            }
        }

        return 1;
    }

    public int GetMesNoSyncHoldReq(HoldReqInfoBean Info)
    {
        var db = Db.Default;
        var result = 0;
        var tireLock = Info.ErrCd == "00" ? 0 : 1;
        var mesProduct = db.MesProducts.Where(t => t.BcdNo == Info.BcdNo).FirstOrDefault();
        if (mesProduct is not null)
        {
            mesProduct.ErrCd = Info.ErrCd;
            mesProduct.ErrDesc = Info.ErrDesc;
            mesProduct.TireLock = tireLock;
            //db.MesProducts.Update(mesProduct);
            db.SaveChanges();
            result += 1;
        }


        var prodProduct = db.ProdProducts.Where(t => t.ProductCode == Info.BcdNo).OrderByDescending(t => t.CreateTime).FirstOrDefault();
        if (prodProduct is not null)
        {
            prodProduct.ErrCd = Info.ErrCd;
            prodProduct.ErrDesc = Info.ErrDesc;
            prodProduct.TireLock = tireLock;
            prodProduct.UpdateTime = DateTime.Now;
            db.SaveChanges();
            result += 1;
        }


        return result;

    }

    public TireInfo GetMesProductByProductCode(string productCode)
    {

        var result = _MesProduct.GetInfoByBarcode(productCode);
        return result;
    }


    /// <summary>
    /// 从规格描述中获取字母R后面的数字，即为尺寸
    /// </summary>
    /// <returns></returns>
    public static double GetSize(string materialDesc)
    {
        Match match = Regex.Match(materialDesc, @"R(\d+(?:\.\d+)?)");
        if (match.Success)
        {
            string text = match.Groups[1].Value;
            if (double.TryParse(text, out double size))
            {
                return size;
            }
        }

        return 0;
    }


}
